package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.QuestionAnswerDto;
import com.plz.afmt.dto.QuestionAnswerQueryDto;
import com.plz.afmt.entity.QuestionAnswerEntity;
import com.plz.afmt.mapper.QuestionAnswerMapper;
import com.plz.afmt.service.QuestionAnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.QuestionAnswerTransfer;
import com.plz.afmt.vo.QuestionAnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 问答记录表
 *
 * @author jzc
 */
@Service
public class QuestionAnswerServiceImpl extends ServiceImpl<QuestionAnswerMapper, QuestionAnswerEntity> implements QuestionAnswerService {
    @Autowired
    private QuestionAnswerTransfer questionAnswerTransfer;

    @Override
    public PagerResult<QuestionAnswerVo> pageQuery(QuestionAnswerQueryDto dto) {
        QueryWrapper<QuestionAnswerEntity> queryWrapper = PageCommonUtil.dtoToWrapper(QuestionAnswerEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<QuestionAnswerEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<QuestionAnswerVo> voPagerResult = questionAnswerTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public QuestionAnswerEntity addOrUpdate(QuestionAnswerDto dto) {
            QuestionAnswerEntity entity = new QuestionAnswerEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<QuestionAnswerEntity> addOrUpdateList(List<QuestionAnswerDto> dto) {
        List<QuestionAnswerEntity> entitys = questionAnswerTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<QuestionAnswerVo> listFor(QuestionAnswerQueryDto dto) {
        QueryWrapper<QuestionAnswerEntity> queryWrapper = PageCommonUtil.dtoToWrapper(QuestionAnswerEntity.class, dto);
        List<QuestionAnswerEntity> list = this.list(queryWrapper);
        List<QuestionAnswerVo> vos = questionAnswerTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public QuestionAnswerVo detail(Long id) {
            QuestionAnswerEntity entity = this.getById(id);
            QuestionAnswerVo vo = new QuestionAnswerVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public Boolean add(Long userId, String question, String answer) {
        QuestionAnswerEntity entity = new QuestionAnswerEntity();
        entity.setUserId(userId);
        entity.setQuestion(question);
        entity.setAnswer(answer);
        entity.setCreateTime(LocalDateTime.now());
        return this.save(entity);
    }

}



